<template>
	<view class="page-content">
		<view class="back-content">
			<view class="flex-content">
				<view class="header-titleV">请输入短信验证码</view>
				<view class="header-tip">短信验证码已发送至{{mobile?mobile:""}}</view>

				<view class="pswInputV">
					<zm-verify-input :codeLength="infoData.codeLength" :code="smsCode" />
				</view>
				<view class="regTip re-send" v-if="!infoData.codeLoading">
					<view @click="sendSMS">{{ShowBtnText}}</view>
				</view>
				<view class="regTip timeNum" v-if="infoData.codeLoading">
					<view>{{infoData.codeCurTime}}s后重新发送</view>
				</view>
				<button class="submitBtn" type="primary"
					@click="confirmRequest">{{confirming == true?"确认中...":"确认"}}</button>
			</view>
			<zm-keyboard-number @keyinput="onKeysInput" @del="onKeysDel" />
		</view>
	</view>
</template>

<script>
	export default {
		name: "zm-captcha-verification-view",
		components: {
		},

		props: {
			mobile: { // 手机号
				default: String,
				type: '',
			},
			sending: { // 验证码请求发送中
				default: false,
				type: Boolean
			},
			confirming: { // 验证码确认中
				default: false,
				type: Boolean
			},

		},

		data: function() {
			return {
				smsCode: '', // 验证码
				infoData: {
					codeTimer: null, // 定时器
					codeLoading: true, //验证码倒计时显示
					codeDownTime: 60, //验证码倒计时 - 起始值
					codeCurTime: 60, //验证码倒计时 - 当前值
					codeLength: 4, //验证码长度设置
				}
			}
		},


		computed: {
			// 发送按钮文案获取
			ShowBtnText() {
				return this.infoData.codeCurTime == 0 ? "重新获取验证码" : "获取验证码";
			},
		},

		// 挂载到实例上去之后调用
		mounted() {
			this.sendSMS();
		},
		methods: {
			// 键盘 - 监听键盘变化
			onKeysInput(item) {
				if (this.smsCode.length > this.infoData.codeLength - 1) {
					return false;
				}
				this.smsCode = this.smsCode + item.key;
			},

			// 键盘 - 点击删除按钮
			onKeysDel(item) {
				let temp = this.smsCode.split('') || [];
				temp.pop();
				this.smsCode = temp.join("");
			},

			// 发送验证码 或点击重新发送
			sendSMS() {
				if (this.sending) {
					return;
				}

				// 1、置空旧的输入的验证码
				this.smsCode = '';
				// 2、发送验证码请求
				this.$emit('getVerifyCode');
			},

			// 开启计时器
			startTimer() {
				this.infoData.codeCurTime = this.infoData.codeDownTime;
				this.infoData.codeLoading = true;
				let that = this;
				this.infoData.codeTimer = setInterval(function() {
					that.infoData.codeCurTime--;
					if (that.infoData.codeCurTime == 0) {
						that.endTimer();
					}
				}, 1000)
			},


			// 结束计时器
			endTimer() {
				if (this.infoData.codeTimer) {
					clearInterval(this.infoData.codeTimer);
					this.infoData.codeTimer = null;
					this.infoData.codeLoading = false;
					this.infoData.codeCurTime = 0;
				}
			},

			// 注册请求处理
			confirmRequest() {
				if (this.registing) {
					return
				}
				if (this.smsCode.length < this.infoData.codeLength) {
					uni.showToast({
						title: "请输入验证码",
						icon: 'none'
					})
					return;
				}

				// 结束倒计时
				this.endTimer();
				this.$emit("confirm", this.smsCode);
			},
		},
	}
</script>

<style lang="scss" scoped>
	.page-content {
		width: 100%;
		height: 100%;
		position: fixed;

		.back-content {
			position: relative;
			margin: 30rpx;
			padding: 30rpx;
			//  底部安全高度，顺序不要乱
			// padding-bottom: constant(safe-area-inset-bottom);
			// padding-bottom: env(safe-area-inset-bottom);
			height: calc(100vh - 400rpx - constant(safe-area-inset-bottom) * 1.5 - (88rpx + var(--status-bar-height)) - 60rpx);
			height: calc(100vh - 400rpx - env(safe-area-inset-bottom) * 1.5 - (88rpx + var(--status-bar-height)) - 60rpx);
			border-radius: 20rpx;
			box-shadow: 0 2px 10px 0 rgba(0, 0, 0, 0.05);
			background: #FFFFFF;

			.flex-content {
				margin: 0rpx;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;


				.header-titleV {
					width: 100%;
					margin-top: 10rpx;
					text-align: center;
					color: #1c1c1c;
					font-size: 56rpx;
				}

				.pswInputV {
					width: 100%;
					margin-top: 100rpx;
				}

				.header-tip {
					width: 100%;
					padding-top: 30rpx;
					text-align: center;
					color: #999999;
					font-size: 28rpx;
				}

				.regTip {
					width: 100%;
					margin-top: 60rpx;
					margin-right: 40rpx;
					font-size: 28rpx;
					text-align: right;


					&.re-send {
						color: #ffd100;
					}

					&.timeNum {
						color: #999999;
					}
				}

				.submitBtn {
					width: 100%;
					margin-top: 120rpx;
					height: 88rpx;
					border-radius: 44rpx;
					background: linear-gradient(90deg, #FFE061 0%, #FFC929 100%);
					color: #1C1C1C;
					font-size: 36rpx;
					font-weight: bold;
				}

				.submitBtn::after {
					border: none;
				}
			}
		}
	}
</style>
